package com.gxa.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxa.common.R;
import com.gxa.pojo.Notice;
import com.gxa.service.NoticeService;
import com.gxa.service.impl.NoticeServiceImlpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/notice/mod")
public class ModNoticeServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取表单数据
        String notice_num = request.getParameter("notice_num");
        String notice_name = request.getParameter("notice_name");
        String notice_type = request.getParameter("notice_type");
        String notice_person = request.getParameter("notice_person");
        String notice_content = request.getParameter("notice_content");
        int id = Integer.parseInt(request.getParameter("id"));

        //创建Notice实体类
        Notice notice = new Notice();
        notice.setNotice_num(notice_num);
        notice.setNotice_name(notice_name);
        notice.setNotice_type(notice_type);
        notice.setNotice_person(notice_person);
        notice.setNotice_content(notice_content);
        notice.setId(id);
        //获取系统时间
        Date date = new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1 = format.format(date);

        //调用service
        NoticeService noticeService = new NoticeServiceImlpl();
        R r = null;
        try {
            r = noticeService.mod(notice, date1);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //响应jsonResult
        response.setContentType("application/json; charset=UTF-8");
        response.getWriter().write(JSONObject.toJSONString(r));
    }
}
